#!/usr/bin/env python
# -*- coding:utf-8 -*-

'''
@Author : JoeyGosson
@Time   : 2019/10/08 14:40
@File   : proxy_pool.py
'''
import redis, re, threading
import os, sys, random
sys.path.append(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
from config import settings
from apscheduler.schedulers.background import BackgroundScheduler
from modules.log_model import create_logger



log = create_logger(isPrint=settings.PRINT_LOG)
lock = threading.Lock()
hash_info = list()
domain_dict = dict()
redis_connect = None



def get_redis_ips():
    global hash_info
    hash_info = redis_connect.hkeys(settings.PROXY_REDIS_HASH_NAME)
    return list(hash_info)

def cron_run():
    global redis_connect
    if not redis_connect:
        redis_connect = redis.Redis(host=settings.PROXY_REDIS_HOST, password=settings.PROXY_REDIS_PASSWOLRD,
                        port=settings.PROXY_REDIS_PORT, db=settings.PROXY_REDIS_DB,
                        decode_responses=settings.PROXY_REDIS_DECODE, max_connections=settings.REDIS_MAX_CONNECTION)

    # 初始化本地代理池一次
    get_redis_ips()

    scheduler = BackgroundScheduler()
    scheduler.add_job(get_redis_ips, 'cron', second=settings.GET_PROXY_CYCLE)
    try:
        scheduler.start()
    except (KeyboardInterrupt, SystemExit):
        pass



def get_proxy(url):
    ip = ""
    pat = r"^http[s]?://(.*?)[/].*?$"
    try:
        lock.acquire()  #获取锁
        domain = re.compile(pat).findall(url)[0]
        if domain not in domain_dict:
            domain_dict[domain] = 0
        index = domain_dict[domain]
        if index >= len(hash_info):
            index = 0
        ip = hash_info[index]
        domain_dict[domain] = index + 1
    except Exception as e:
        log.debug("定时代理模块出现错误:{}".format(e))
    finally:
        lock.release() #释放锁
        if settings.SHOW_PROXY:
            log.info(ip)
        return ip

def get_proxy_random():
    return random.choice(hash_info)
